package com.itic.mall.system.users.userHelp.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.mall.system.inter.users.userHelp.persistence.dao.UsersHelpDao;
import com.itic.mall.system.inter.users.userHelp.persistence.model.UsersHelp;
import com.itic.mall.system.inter.users.userHelp.persistence.model.UsersHelpExample;
import com.itic.mall.system.inter.users.userHelp.persistence.model.UsersHelpExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


@Service
@Transactional
public class UsersHelpService
     extends CrudService<UsersHelpDao,UsersHelp,UsersHelpExample> {
    @Autowired
    private UsersHelpDao usersHelpDao;
    /**
     * 查询帮助的用户
     * @param userId 被帮助人的userId
     * @return
     */
    public List<UsersHelp>  findHelpUserByUserId(String userId){
    	UsersHelpExample example = new UsersHelpExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	List<UsersHelp> helpUsers = this.list(example);
    	return helpUsers;
    }
    
    /**
     * 计算助理的年利率
     * @return
     */
    public double calHelpRate () {
    	double rate = Math.random()*(2-0.1)+0.1;
		BigDecimal   b   =   new   BigDecimal(rate);
		double   f1   =   b.setScale(3,   BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
    }
    /**
     * 根据unionId查询微信用户是否助理过  
     * @param unionId
     * @return
     */
    public List<UsersHelp>  findHelpUserByUnionId(String unionId){
    	UsersHelpExample example = new UsersHelpExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUnionidEqualTo(unionId);
    	List<UsersHelp> helpUsers = this.list(example);
    	return helpUsers;
    }
    
   public String addUserHelp(String userId,String name,String unionId,double rate) {
	   UsersHelp usersHelp = new UsersHelp();
	   usersHelp.setName(name);
	   usersHelp.setUnionid(unionId);
	   usersHelp.setUserId(userId);
	   usersHelp.setRate(rate);
	   usersHelp.setIsCal("0");
	   String result = this.add(usersHelp);
	   return result;
   }
   
   public List<UsersHelp>  findHelpUserByUnionId(String unionId,String userId){
   	UsersHelpExample example = new UsersHelpExample();
   	Criteria criteria = example.createCriteria();
   	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
   	criteria.andUnionidEqualTo(unionId);
   	criteria.andUserIdEqualTo(userId);
   	List<UsersHelp> helpUsers = this.list(example);
   	return helpUsers;
   }
   
   /**
    * 查询用户未计算生息助力的人 
    * @param userId
    * @return
    */
   public List<UsersHelp>  findNotCalUsersHelp(String userId){
	   UsersHelpExample example = new UsersHelpExample();
	   Criteria criteria = example.createCriteria();
	   criteria.andIsEffectiveEqualTo(DBConst.TRUE);
	   criteria.andIsCalEqualTo("0");
	   criteria.andUserIdEqualTo(userId);
	   List<UsersHelp> helpUsers = this.list(example);
	   	return helpUsers;
   }
   
   public double getUsersHelpTotalRate(String userId) {
	   List<UsersHelp> helpUsers = this.findNotCalUsersHelp(userId);
	   double total = 0;
	   for(UsersHelp usersHelp : helpUsers)
		   total = total + usersHelp.getRate();
	   return total;
   }
}